<template>
  <div class="pop-dialog-container">
    <div class="pop-dialog-main">
      <el-form label-position="right" :model="applicationRecordForm" :label-width="formLabelWidth" ref="refApplicationRecordForm" :rules="rules">
        <el-row>
          <el-col :span="11">
            <el-form-item label="企业名称" prop="enterpriseName">
              <el-input v-model="applicationRecordForm.enterpriseName" placeholder="请选择企业名称" readonly="readonly">
                <i slot="suffix" class="el-input__icon el-icon-search" @click="findEnterprise"></i>
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="11">
            <el-form-item label="招聘岗位名称" prop="jobDescriptionName">
              <el-input v-model="applicationRecordForm.jobDescriptionName" placeholder="请选择招聘岗位名称" readonly="readonly">
                <i slot="suffix" class="el-input__icon el-icon-search" @click="findJobDescription"></i>
              </el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="11">
            <el-form-item label="应聘者姓名" prop="candidateName">
              <el-input v-model="applicationRecordForm.candidateName" placeholder="请选择应聘者姓名" readonly="readonly">
                <i slot="suffix" class="el-input__icon el-icon-search" @click="findCandidate"></i>
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="11">
            <el-form-item label="状态" prop="status">
              <el-select v-model="applicationRecordForm.status" placeholder="请选择状态">
                <el-option label="申请" :value="10"></el-option>
                <el-option label="面试邀约" :value="20"></el-option>
                <el-option label="录用" :value="30"></el-option>
                <el-option label="拒绝" :value="90"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </div>
    <div class="pop-dialog-footer">
      <el-row>
        <el-col :span="11" :offset="6">
          <div class="form-button-container">
            <el-button type="primary" @click="onSave">保存</el-button>
            <el-button @click="onReset">重置</el-button>
            <el-button type="primary" @click="onBack">关闭</el-button>
          </div>
        </el-col>
      </el-row>
    </div>
    <el-dialog :visible.sync="enterpriseSelectorVisible" v-if="enterpriseSelectorVisible" title="企业信息选择器(双击行选中)" :top="dialogTop" :width="dialogWidth" :append-to-body="true">
      <enterprise-selector @enterprise-selected-event="handleEnterpriseSelectedEvent">
      </enterprise-selector>
    </el-dialog>
    <el-dialog :visible.sync="jobDescriptionSelectorVisible" v-if="jobDescriptionSelectorVisible" title="招聘岗位选择器(双击行选中)" :top="dialogTop" :width="dialogWidth" :append-to-body="true">
      <job-description-selector @job-description-selected-event="handleJobDescriptionSelectedEvent">
      </job-description-selector>
    </el-dialog>
    <el-dialog :visible.sync="candidateSelectorVisible" v-if="candidateSelectorVisible" title="应聘者基本信息选择器(双击行选中)" :top="dialogTop" :width="dialogWidth" :append-to-body="true">
      <candidate-selector @candidate-selected-event="handleCandidateSelectedEvent">
      </candidate-selector>
    </el-dialog>
  </div>
</template>

<script>
import applicationRecordApi from '@/api/applicationRecordApi'
import EnterpriseSelector from "@/views/enterprise/EnterpriseSelector.vue";
import JobDescriptionSelector from "@/views/jobDescription/JobDescriptionSelector.vue";
import CandidateSelector from "@/views/candidate/CandidateSelector.vue";
export default {
  name: 'ApplicationRecordAdd',
  components: {EnterpriseSelector,JobDescriptionSelector,CandidateSelector,},
  props: ['id'],
  mixins: [
  ],
  data () {
    return {
      applicationRecordForm: {
        enterpriseId: '',
        enterpriseName: '',
        jobDescriptionId: '',
        jobDescriptionName: '',
        candidateId: '',
        candidateName: '',
        status: ''
      },
      rules: {
        enterpriseName: [
          { required: true, message: '请输入企业名称', trigger: 'blur' }
        ],
        jobDescriptionName: [
          { required: true, message: '请输入招聘岗位名称', trigger: 'blur' }
        ],
        candidateName: [
          { required: true, message: '请输入应聘者姓名', trigger: 'blur' }
        ],
        status: [
          { required: true, message: '请输入状态', trigger: 'blur' }
        ],
      },
      enterpriseSelectorVisible: false,
      jobDescriptionSelectorVisible: false,
      candidateSelectorVisible: false,
    }
  },
  created () {
    if (this.id) {
      applicationRecordApi.findById(Number(this.id))
        .then(resp => {
          if (resp && resp.code === 1) {
            this.applicationRecordForm = resp.data
          } else {
            this.$message.error('获取数据失败')
          }
        })
        .catch(error => {
          console.error('获取数据失败:', error)
        })
    }
  },
  methods: {
    onReset () {
      this.applicationRecordForm = {
        enterpriseId: '',
        enterpriseName: '',
        jobDescriptionId: '',
        jobDescriptionName: '',
        candidateId: '',
        candidateName: '',
        status: ''
      }
      this.$emit('reset-application-record-add-event')
    },
    onSave () {
      this.$refs.refApplicationRecordForm.validate((valid) => {
        if (valid) {
          applicationRecordApi.save(this.applicationRecordForm)
            .then((resp) => {
              if (resp && resp.code === 1) {
                this.$message.success('保存成功')
                this.$emit('close-application-record-add-event', {search: true})
              } else {
                this.$message.error('保存失败:' + (resp.msg ? resp.msg : ''))
              }
            })
            .catch(error => {
              console.log(error)
              this.$message.error('保存失败')
            })
        } else {
          return false
        }
      })
    },
    findEnterprise () {
      this.enterpriseSelectorVisible = true
    },
    handleEnterpriseSelectedEvent (selectedEnterprise) {
      this.enterpriseSelectorVisible = false
      if (selectedEnterprise && 'enterpriseId' in selectedEnterprise && this.applicationRecordForm.enterpriseId !== selectedEnterprise['enterpriseId']) {
        this.applicationRecordForm.enterpriseId = selectedEnterprise['enterpriseId']
        this.applicationRecordForm.enterpriseName = selectedEnterprise['enterpriseName']
      }
    },
    findJobDescription () {
      this.jobDescriptionSelectorVisible = true
    },
    handleJobDescriptionSelectedEvent (selectedJobDescription) {
      this.jobDescriptionSelectorVisible = false
      if (selectedJobDescription && 'jobDescriptionId' in selectedJobDescription && this.applicationRecordForm.jobDescriptionId !== selectedJobDescription['jobDescriptionId']) {
        this.applicationRecordForm.jobDescriptionId = selectedJobDescription['jobDescriptionId']
        this.applicationRecordForm.jobDescriptionName = selectedJobDescription['jobDescriptionName']
      }
    },
    findCandidate () {
      this.candidateSelectorVisible = true
    },
    handleCandidateSelectedEvent (selectedCandidate) {
      this.candidateSelectorVisible = false
      if (selectedCandidate && 'candidateId' in selectedCandidate && this.applicationRecordForm.candidateId !== selectedCandidate['candidateId']) {
        this.applicationRecordForm.candidateId = selectedCandidate['candidateId']
        this.applicationRecordForm.candidateName = selectedCandidate['candidateName']
      }
    },
    onBack () {
      this.$emit('close-application-record-add-event')
    }
  }
}
</script>

<style lang="scss">
</style>